import resolve from '@rollup/plugin-node-resolve';
import vuePlugin from 'rollup-plugin-vue';
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer';
import copy from 'rollup-plugin-copy';
import url from 'postcss-url';
import typescript from '@rollup/plugin-typescript';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  input: './packages/components/index.js',
  output: [
    {
      file: 'dist/es.js',
      name: 'TestUI',
      format: 'es'
    },
    {
      file: 'dist/cjs.js',
      name: 'TestUI',
      format: 'cjs',
      exports: 'named'
    },
    {
      file: 'dist/umd.js',
      name: 'TestUI',
      format: 'umd',
      exports: 'named',
      globals: {
        vue: 'Vue'
      }
    }
  ],
  plugins: [
    resolve(),
    vuePlugin({
      // Providing fs implementation to the plugin
      compiler: {
        // A simple fs implementation for the compiler
        fileExists: (file) => fs.existsSync(file),
        readFile: (file) => fs.readFileSync(file, 'utf-8'),
      },
    }),
    typescript({
      tsconfig: path.resolve(__dirname, './tsconfig.json'),
      // This helps rollup-plugin-typescript find the config file
    }),
    postcss({
      extract: 'theme-chalk/style.css', // 将css提取到单独的文件中
      plugins: [
        autoprefixer(),
        url({
          url: 'copy',
          assetsPath: 'fonts'
        })
      ]
    }),
    copy({
      targets: [
        { src: 'packages/theme-chalk/fonts/*', dest: 'dist/theme-chalk/fonts/' }
      ]
    })
  ],
  external: ['vue'] // 依赖模块
};
